package com.lag.transfer.service.impl;

import com.lag.transfer.annotation.MyDefinedAutowired;
import com.lag.transfer.annotation.MyDefinedService;
import com.lag.transfer.annotation.MyDefinedTransactional;
import com.lag.transfer.dao.AccountDao;
import com.lag.transfer.pojo.Account;
import com.lag.transfer.service.TransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author June
 * @since 2020年12月
 */
@MyDefinedService("transferService")
public class TransferServiceImpl implements TransferService {

    @MyDefinedAutowired
    private AccountDao accountDao;

    @MyDefinedTransactional
    @Override
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {
        System.out.println("执行业务转账逻辑...");
        Account from = accountDao.queryAccountByCardNo(fromCardNo);
        Account to = accountDao.queryAccountByCardNo(toCardNo);

        from.setMoney(from.getMoney() - money);
        to.setMoney(to.getMoney() + money);

        accountDao.updateAccountByCardNo(to);
        //int c = 1/0;
        accountDao.updateAccountByCardNo(from);
    }
}
